/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pro.unal.edu.filter;

import java.awt.Color;

/**
 *
 * @author Carlos
 */
public class BilinealFilter implements MyFilter {

    @Override
    public void doFilter(MyPoint[][] newImage, Color[][] colors) {
        for (int i = 0; i < newImage.length; i++) {
            for (int j = 0; j < newImage[0].length; j++) {
                if (newImage[i][j] != null) {
                    /* Get the 4 colors*/
                    Color cU = new Color(255, 255, 255);
                    Color cD = new Color(255, 255, 255);
                    Color cL = new Color(255, 255, 255);
                    Color cR = new Color(255, 255, 255);
                    try {
                        cU = newImage[i][j - 1].getColor();
                    } catch (Exception e) {
                    }
                    try {
                        cD = newImage[i][j + 1].getColor();
                    } catch (Exception e) {
                    }
                    try {
                        cL = newImage[i][j].getColor();
                    } catch (Exception e) {
                    }
                    try {
                        cR = newImage[i + 2][j].getColor();
                    } catch (Exception e) {
                    }
                    /*Asign Color to the neighbor*/
                    int red = (cU.getRed() + cD.getRed() + cL.getRed() + cR.getRed()) / 4;
                    int blue = (cU.getBlue() + cD.getBlue() + cL.getBlue() + cR.getBlue()) / 4;
                    int green = (cU.getGreen() + cD.getGreen() + cL.getGreen() + cR.getGreen()) / 4;
                    try {
                        newImage[i + 1][j] = new MyPoint(j, j, new Color(red, green, blue));
//                        newImage[i + 2][j] = new MyPoint(j, j, newImage[i][j].getColor());
                    } catch (Exception e) {
                    }
//                    Color c = new Color(238, 238, 238);
//                    newImage[i][j] = new MyPoint(j, j, c);
                }
            }
        }
    }

    @Override
    public Color[][] doFilter(Color[][] actualImage) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
